//
//  SCNCircleLoadView.swift
//  Swift3UIDemo
//
//  Created by 王家旭 on 2017/3/16.
//  Copyright © 2017年 Riluee. All rights reserved.
//

import UIKit

class SCNCircleLoadView: UIView {
    // 底层圈
    let circleBackLayer = CAShapeLayer()
    // 显示圈
    let circlePathLayer = CAShapeLayer()
    // 半径
    var circleRadius: CGFloat?
    // 进度显示条
    var progressTextL: UILabel = {
       
        let lab = UILabel()
        lab.textColor = SCNConstants.colorWithHexString("17a9f2")
        lab.textAlignment = .center
        lab.font = UIFont.systemFont(ofSize: 13.5)
        lab.text = "0%"
        return lab
    }()
    // 进度
    var progress: CGFloat = 0 {

        didSet{
            if (progress > 100) {
                circlePathLayer.strokeEnd = 1
            } else if (progress < 0) {
                circlePathLayer.strokeEnd = 0
            } else {
                circlePathLayer.strokeEnd = progress * 0.01
            }
            
            progressTextL.text = String(format: "%.0f%%", progress)
            progressTextL.snp.makeConstraints { (make) in
                make.height.equalTo(15)
                make.center.equalToSuperview()
            }
            // 在这里做递增的动画
            /*
            let animation = CABasicAnimation(keyPath: "strokeEnd")
            animation.fromValue = oldValue
            animation.toValue = progress * 0.01
            animation.autoreverses = false
            animation.duration = 1.0
                        
            circlePathLayer.add(animation, forKey: "circle")
             */
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.circleRadius = 32
        configuration()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configuration() {
        backgroundColor = UIColor.clear
        /*
         strokeStart、strokeEnd
         这两个属性的值在0~1之间，0代表Path的开始位置，1代表Path的结束位置。是一种线性递增关系。strokeStart默认值为0，strokeEnd默认值为1。这两个属性都支持动画。
         */
        //
        circlePathLayer.frame = bounds
        circlePathLayer.lineWidth = 6
        circlePathLayer.fillColor = UIColor.clear.cgColor
        circlePathLayer.strokeColor = SCNConstants.colorWithHexString("17a9f2").cgColor
        circlePathLayer.strokeEnd = 0
        //
        circleBackLayer.frame = bounds
        circleBackLayer.lineWidth = 6
        circleBackLayer.fillColor = UIColor.clear.cgColor
        circleBackLayer.strokeColor = SCNConstants.colorWithHexString("e0eff6").cgColor
        circleBackLayer.strokeEnd = 1
        
        
        layer.addSublayer(circleBackLayer)
        layer.addSublayer(circlePathLayer)
        self.addSubview(progressTextL)
        
    }
    
    func circleFrame() -> CGRect {
        var circleFrame = CGRect(x: 0, y: 0, width: 2 * circleRadius!, height: 2 * circleRadius!)
        circleFrame.origin.x = circlePathLayer.bounds.midX - circleFrame.midX
        circleFrame.origin.y = circlePathLayer.bounds.midY - circleFrame.midY
        return circleFrame
    }
    // CAShapeLayer + UIBezierPath 画图形
    func circlePath() -> UIBezierPath {
        return UIBezierPath(ovalIn: circleFrame())
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //
        circlePathLayer.frame = bounds
        circlePathLayer.path = circlePath().cgPath
        //
        circleBackLayer.frame = bounds
        circleBackLayer.path = circlePath().cgPath
      
    }
    

    
}
